package com.jn.book.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.book.R;
import com.jn.book.databinding.CatalogItemBinding;
import com.jn.book.entity.CatalogEntity;

import java.util.List;

public class CatalogAdapter extends RecyclerView.Adapter<CatalogAdapter.ViewHolder> {
    private List<CatalogEntity.ResultDTO> mList;
    private OnItemClickListener mListener;

    public CatalogAdapter(List<CatalogEntity.ResultDTO> list) {
        mList = list;
    }

    public void updateData(List<CatalogEntity.ResultDTO> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        CatalogItemBinding binding = CatalogItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CatalogEntity.ResultDTO catalog = mList.get(position);
        holder.binding.setData(catalog);
        holder.binding.setListener(new CatalogItemClickListener());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CatalogItemBinding binding;

        public ViewHolder(CatalogItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(String catalog_id);
    }

    public class CatalogItemClickListener {
        public void onClick(View view, CatalogEntity.ResultDTO catalog) {
            mListener.onItemClick(catalog.getId());
        }
    }
}
